﻿#ifndef IGRPCSERVICE_H
#define IGRPCSERVICE_H

#include "osgi-predefined.h"
#include "IService.h"

#ifndef NULL
#ifdef __cplusplus
#define NULL 0
#else /* __cplusplus */
#define NULL ((void *)0)
#endif /* __cplusplus */
#endif /* NULL */

#ifdef _WINDOWS
#define IGRPC_DLL __declspec(dllexport)
#else
#define IGRPC_DLL __declspec(dllimport)
#endif

#ifdef _WINDOWS
#define OSGI_API __cdecl
#include <sys/types.h>
#include "windows.h"

#else

#include <sys/types.h>
#define IGRPC_DLL __attribute__((visibility("default")))
#define __int64 int64_t
#define OSGI_API __attribute__((__cdecl__))
#define _cdecl __attribute__((__cdecl__))
#define __cdecl __attribute__((__cdecl__))
#endif

#define OSGI_INTERFACE struct
#define OSGI_SUCCESS 0
#define OSGI_ERROR -1
#define NULL                0


#include <iostream>
#include <memory>
#include <string>
#include <thread>

#include "grpcpp/ext/proto_server_reflection_plugin.h"
#include "grpcpp/grpcpp.h"
#include "grpcpp/health_check_service_interface.h"
#include "grpc/support/log.h"
#include "grpc/grpc.h"
#include "grpcpp/security/server_credentials.h"
#include "grpcpp/impl/codegen/sync_stream.h"
#include "grpcpp/server.h"
#include "grpcpp/server_builder.h"
#include "grpcpp/server_context.h"

//using namespace std;
using grpc::Server;
using grpc::ServerBuilder;
using grpc::ServerContext;
using grpc::ServerReader;
using grpc::ServerReaderWriter;
using grpc::ServerWriter;
using grpc::Status;
using grpc::ServerAsyncResponseWriter;
using grpc::ServerCompletionQueue;


namespace MicroKernelRuntime
{

#define IID_GRPC_Server "IID_GRPC_Server"
#define SID_GRPC_Server "SID_GRPC_Server"

#define IID_GRPC_TestServer "IID_GRPC_TestServer"
#define SID_GRPC_TestServer "SID_GRPC_TestServer"

#define IID_GRPC_TestClient "IID_GRPC_TestClient"
#define SID_GRPC_TestClient "SID_GRPC_TestClient"

OSGI_INTERFACE IGRPCService : public IService
{
public:
    //static IGRPCServer * Instance();

    //gRPC同步调用初始化
    //virtual void OSGI_API gRPCSynServerInit(const char* conf_filename, const char* PluginName) = 0;
    virtual void OSGI_API gRPCSynServerInit(char *server_address,bool enablepoll,int min_poll,int max_poll) = 0; /*配置文件，须传递服务的IP和端口,插件名称*/
    //gRPC同步服务的注册
    virtual void OSGI_API gRPCSynServerRegister(grpc::Service * service) = 0;    /*服务的注册，如果使用同一端口IP，需要在一个服务内部依次注册*/
    //gRPC同步服务的启动
    virtual void OSGI_API gRPCSynServerstart() = 0;  /*服务的启动*/

    //gRPC异步调用初始化
    //virtual void OSGI_API gRPCAsynServerInit(const char* conf_filename, const char* PluginName);
    virtual void OSGI_API gRPCAsynServerInit(char * server_address) = 0;   /*配置文件，须传递服务的IP和端口,插件名称*/
    //gRPC异步服务的注册
    virtual void OSGI_API gRPCAsynServerRegister(std::unique_ptr<ServerCompletionQueue>& cq_, grpc::Service * service_) = 0;
    /*服务的注册，如果使用同一端口IP，需要在一个服务内部依次注册*/
    //异步服务的启动之后需要调用使用者的回调,服务使用获取的接口中的服务
    //virtual void OSGI_API gRPCAsynServerstart(std::unique_ptr<Server> server_);
    virtual void OSGI_API gRPCAsynServerstart() = 0;
    //virtual void OSGI_API gRPCAsynServerstart(std::unique_ptr<ServerCompletionQueue> cq_) = 0;
    /*服务的注册，如果使用同一端口IP，需要在一个服务内部依次注册*/
    //异步的销毁，因为cq队列和服务不在同一个函数内；
    //virtual void OSGI_API gRPCAsynServerDestory() = 0;
    virtual void OSGI_API gRPCAsynServerDestory(std::unique_ptr<ServerCompletionQueue>& cq_) = 0;


    //同步方式使用,已合并到初始化函数
    //virtual void OSGI_API setminpollnum(int minnum) = 0;
    //virtual void OSGI_API setmaxpollnum(int maxnum) = 0;
    //virtual void OSGI_API enablepoll(bool enbleflag) = 0;

/*
private:
    static IGRPCService * m_pInstance;
    std::string server_address;
    ServerBuilder m_builder;
    std::unique_ptr<Server> m_server;
    std::unique_ptr<ServerCompletionQueue> m_cq;
    bool m_threadpoll = false;  //是否启用多线程
    int min_poll = 0;
    int max_poll = 0;
*/
};


}

#endif
